<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编号" prop="serial">
        <el-input v-model="queryParams.serial" placeholder="请输入编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="物品名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入物品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="维修进度" prop="plan">
        <el-input v-model="queryParams.plan" placeholder="请输入维修进度" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="维修费用" prop="money">
        <el-input v-model="queryParams.money" placeholder="请输入维修费用" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建时间" prop="startTime">
        <el-date-picker clearable v-model="queryParams.startTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择维修创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="用户账号" prop="uhao">
        <el-input v-model="queryParams.uhao" placeholder="请输入用户账号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户姓名" prop="uName">
        <el-input v-model="queryParams.uName" placeholder="请输入用户姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户电话" prop="uPhone">
        <el-input v-model="queryParams.uPhone" placeholder="请输入用户电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="维修账号" prop="mhao">
        <el-input v-model="queryParams.mhao" placeholder="请输入维修账号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="维修姓名" prop="mName">
        <el-input v-model="queryParams.mName" placeholder="请输入维修姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="维修电话" prop="mPhone">
        <el-input v-model="queryParams.mPhone" placeholder="请输入维修电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:goods:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['system:goods:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['system:goods:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:goods:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="50" label="序号" align="center"> </el-table-column>
      <el-table-column label="编号" align="center" prop="serial" />
      <el-table-column label="物品名称" align="center" prop="name" />
      <!-- <el-table-column label="物品分类" align="center" prop="type" /> -->
      <el-table-column label="维修进度" align="center" prop="plan" >
        <template slot-scope="scope">
          <span>{{scope.row.plan+'%' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修费用" align="center" prop="money" />
      <el-table-column label="维修创建时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户账号" align="center" prop="uhao" />
      <el-table-column label="用户姓名" align="center" prop="uName" />
      <el-table-column label="用户电话" align="center" prop="uPhone" />
      <el-table-column label="维修账号" align="center" prop="mhao" />
      <el-table-column label="维修姓名" align="center" prop="mName" />
      <el-table-column label="维修电话" align="center" prop="mPhone" />
      <el-table-column label="是否支付" align="center" prop="pay">
        <template slot-scope="scope">
          <el-tag :type="scope.row.pay==1?'success':'danger'">{{scope.row.pay==1?'已支付':'未支付'}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:goods:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:goods:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改维修记录,管理员可见权限对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="物品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入物品名称" />
        </el-form-item>
        <el-form-item label="维修进度" prop="plan">
          <el-input-number v-model="form.plan"  :max="100" placeholder="请输入维修进度"></el-input-number>
        </el-form-item>
        <el-form-item label="维修费用" prop="money">
          <el-input v-model="form.money" placeholder="请输入维修费用" />
        </el-form-item>
        <el-form-item label="创建时间" prop="startTime">
          <el-date-picker clearable v-model="form.startTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择维修创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="用户账号" prop="uhao">
          <el-input v-model="form.uhao" placeholder="请输入用户账号" />
        </el-form-item>
        <el-form-item label="用户姓名" prop="uName">
          <el-input v-model="form.uName" placeholder="请输入用户姓名" />
        </el-form-item>
        <el-form-item label="用户电话" prop="uPhone">
          <el-input v-model="form.uPhone" placeholder="请输入用户电话" />
        </el-form-item>
        <el-form-item label="维修账号" prop="mhao">
          <el-input v-model="form.mhao" placeholder="请输入维修账号" />
        </el-form-item>
        <el-form-item label="维修姓名" prop="mName">
          <el-input v-model="form.mName" placeholder="请输入维修姓名" />
        </el-form-item>
        <el-form-item label="维修电话" prop="mPhone">
          <el-input v-model="form.mPhone" placeholder="请输入维修电话" />
        </el-form-item>
        <el-form-item label="是否支付" prop="pay">
          <el-radio-group v-model="form.pay">
            <el-radio :label="1">已支付</el-radio>
            <el-radio :label="2">未支付</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGoods, getGoods, delGoods, addGoods, updateGoods } from "@/api/system/goods";

export default {
  name: "Goods",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修记录,管理员可见权限表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serial: null,
        name: null,
        type: null,
        plan: null,
        money: null,
        startTime: null,
        uhao: null,
        uName: null,
        uPhone: null,
        mhao: null,
        mName: null,
        mPhone: null,
        pay: null
      },
      // 表单参数
      form: {

      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "物品名称不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "物品分类不能为空", trigger: "change" }
        ],
        money: [
          { required: true, message: "维修费用不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "维修创建时间不能为空", trigger: "blur" }
        ],
        uhao: [
          { required: true, message: "用户账号不能为空", trigger: "blur" }
        ],
        uName: [
          { required: true, message: "用户姓名不能为空", trigger: "blur" }
        ],
        uPhone: [
          { required: true, message: "用户电话不能为空", trigger: "blur" }
        ],
        mhao: [
          { required: true, message: "维修账号不能为空", trigger: "blur" }
        ],
        mName: [
          { required: true, message: "维修姓名不能为空", trigger: "blur" }
        ],
        mPhone: [
          { required: true, message: "维修电话不能为空", trigger: "blur" }
        ],
        pay: [
          { required: true, message: "请选择支付方式", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询维修记录,管理员可见权限列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serial: null,
        name: null,
        type: null,
        plan: null,
        money: null,
        startTime: null,
        uhao: null,
        uName: null,
        uPhone: null,
        mhao: null,
        mName: null,
        mPhone: null,
        pay: 2
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加维修记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoods(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改维修记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGoods(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.serial = Math.random().toString(36).slice(2)
            addGoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除维修记录,管理员可见权限编号为"' + ids + '"的数据项？').then(function () {
        return delGoods(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/goods/export', {
        ...this.queryParams
      }, `goods_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
